package com.chena.modules.app.controller;


import java.util.Date;

import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.chena.common.utils.R;
import com.chena.common.validator.ValidatorUtils;
import com.chena.modules.app.entity.UserEntity;
import com.chena.modules.app.form.RegisterForm;
import com.chena.modules.app.service.UserService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * 注册
 */
@RestController
@RequestMapping("/app")
@Api(tags = "用户注册")
public class AppRegisterController {
    @Autowired
    private UserService userService;

    @PostMapping("register")
    @ApiOperation(value = "注册", notes = "通过手机号和密码注册")
    @ApiImplicitParams({@ApiImplicitParam(name = "mobile", value = "手机号", required = true, paramType = "body"), 
    					@ApiImplicitParam(name = "password", value = "密码", required = true, paramType = "body")})
    public R register(@RequestBody RegisterForm form){
        //表单校验
        ValidatorUtils.validateEntity(form);
        UserEntity user = new UserEntity();
        user.setMobile(form.getMobile());
        user.setUsername(form.getMobile());
        user.setPassword(DigestUtils.sha256Hex(form.getPassword()));
        user.setCreateTime(new Date());
        userService.save(user);

        return R.ok();
    }
}
